package com.njupt.Collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 2506165018
 * @version 1.0
 * @data 2025-07-29 15:52:17
 */
public class Collectors1 {

    public static void main(String[] args) {
        List<Map<String,Object>> deviceList = createDeviceList();
        Map<Object, Integer> linecodeResult = deviceList.stream()
                .collect(Collectors.groupingBy(device -> device.get("LINECODE")
                        , Collectors.collectingAndThen(Collectors.toList(), List::size))
                );

        for(Map.Entry<Object,Integer> entry : linecodeResult.entrySet()){
            String key = (String)entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " " + value);
        }
    }

    // 创建测试数据的方法
    private static List<Map<String, Object>> createDeviceList() {
        List<Map<String, Object>> list = new ArrayList<>();

        // 添加设备1 - 生产线L1
        Map<String, Object> device1 = new HashMap<>();
        device1.put("LINECODE", "L1");
        device1.put("DEVICE", null);
        device1.put("STATUS", "运行中");
        list.add(device1);

        // 添加设备2 - 生产线L1
        Map<String, Object> device2 = new HashMap<>();
        device2.put("LINECODE", "L1");
        device2.put("DEVICE", "D2");
        device2.put("STATUS", "待机");
        list.add(device2);

        // 添加设备3 - 生产线L2
        Map<String, Object> device3 = new HashMap<>();
        device3.put("LINECODE", "L2");
        device3.put("DEVICE", "D3");
        device3.put("STATUS", "维修中");
        list.add(device3);

        // 再添加一些数据
        Map<String, Object> device4 = new HashMap<>();
        device4.put("LINECODE", "L2");
        device4.put("DEVICE", "D4");
        device4.put("STATUS", "运行中");
        list.add(device4);

        Map<String, Object> device5 = new HashMap<>();
        device5.put("LINECODE", "L3");
        device5.put("DEVICE", "D5");
        device5.put("STATUS", "待机");
        list.add(device5);

        return list;
    }
}
